<template>
  <div>
    <div class="box">
      <el-button type="primary" @click="handleAdd">添加作者</el-button>
    </div>
    <div class="box">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column
          prop="authorId"
          label="ID"
          width="80"
        ></el-table-column>
        <el-table-column
          prop="authorName"
          label="作者名称"
        ></el-table-column>
        <el-table-column prop="penName" label="作者网名" width="180"></el-table-column>
        <el-table-column prop="city" label="居住城市" width="180"></el-table-column>
        <el-table-column prop="authorGroup" label="作者分组" width="180"></el-table-column>
        <el-table-column prop="authorRank" label="作者头衔" width="180"></el-table-column>
        <el-table-column prop="createTime" label="发布时间" width="180"></el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="180"></el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              @click="handleEdit(scope.$index, scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加/修改 -->
    <el-dialog title="作者信息" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="作者名称" :label-width="formLabelWidth" prop="authorName">
          <el-input v-model="form.authorName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="作者网名" :label-width="formLabelWidth" prop="penName">
          <el-input v-model="form.penName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="居住地" :label-width="formLabelWidth" prop="city">
          <el-input v-model="form.city" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="作者分组" :label-width="formLabelWidth" prop="authorGroup">
          <el-select v-model="form.authorGroup" placeholder="请选择">
              <el-option
                v-for="item in authorGroups"
                :key="item.groupId"
                :label="item.groupName"
                :value="item.groupId">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="作者头衔" :label-width="formLabelWidth" prop="authorRank">
          <el-input v-model="form.authorRank" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="作者标签" :label-width="formLabelWidth" prop="tags">
          <el-select
              v-model="form.tags"
              multiple
              filterable
              allow-create
              default-first-option
              placeholder="请选择作者标签">
            </el-select>
        </el-form-item>
        <el-form-item label="作者头像" :label-width="formLabelWidth" prop="authorAvatar">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileServer+'/upload/uploadFile'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="form.authorAvatar"
              :src="form.authorAvatar"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="作者介绍" :label-width="formLabelWidth" prop="authorIntro">
          <el-input
            v-model="form.authorIntro"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 10}"
            placeholder="请输入作者介绍"
            >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {uploadServerURL} from '@/config'
  import {addAuthor,setAuthor,delAuthorById,getAuthorById,getAuthorList,getAuthorGroupList} from '@/api/news.js'
  export default {
    name: 'Author',
    data() {
      return {
        uploadFileServer: uploadServerURL,
        tableData: [],
        dialogFormVisible: false,
        formLabelWidth: '120px',
        form: {
          authorId: 0,
          authorName: null,
          penName: null,
          city: null,
          authorGroup: null,
          authorRank: null,
          authorTags: [],
          authorAvatar: null,
          authorIntro: null,
        },
        rules: {
          authorName: [{ required: true, message: '请输入作者名称', trigger: 'blur' }],
          authorGroup: [{ required: true, message: '请输入作者分组', trigger: 'blur' }],
        },
        authorGroups: []
      }
    },
    mounted() {
      this.loadList()
      this.loadGroupList()
    },
    methods: {
      resetForm() {
        this.form = {
          authorId: 0,
          authorName: null,
          penName: null,
          city: null,
          authorGroup: null,
          authorRank: null,
          authorTags: null,
          tags: [],
          authorAvatar: null,
          authorIntro: null,
        }
      },
      loadList() {
        getAuthorList({}).then(res => {
          if(res.code == 200) {
            this.tableData = res.data
          }
        })
      },
      loadGroupList() {
        getAuthorGroupList().then(res => {
          if(res.code == 200) {
            this.authorGroups = res.data
          }
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.form.authorId>0) {
              this.form.authorTags = this.form.tags.join(',')
              setAuthor(this.form).then(res => {
                if(res.code == 200) {
                  this.$message.success("修改成功")
                  this.dialogFormVisible = false
                  this.loadList()
                }
                else {
                  this.$message.error("修改失败")
                }
              })
            }
            else {
              this.form.authorTags = this.form.tags.join(',')
              addAuthor(this.form).then(res => {
                if(res.code == 200) {
                  this.$message.success("添加成功")
                  this.dialogFormVisible = false
                  this.loadList()
                }
                else {
                  this.$message.error("添加失败")
                }
              })
            }
          } else {
            return false;
          }
        });
      },
      handleAdd() {
        this.resetForm()
        this.dialogFormVisible = true
      },
      handleEdit(index, row) {
        this.resetForm()
        let param = {id: row.authorId}
        getAuthorById(param).then(res => {
          if(res.code == 200) {
            this.form = res.data
            this.form.tags = this.form.authorTags.split(',')
            this.dialogFormVisible = true
          }
        })
      },
      handleDelete(index, row) {
        let param = {id: row.authorId}
        delAuthorById(param).then(res => {
          if(res.code == 200) {
            this.$message.success("删除成功")
          }
          else {
            this.$message.error("删除失败")
          }
        })
      },
      handleAvatarSuccess(res, file) {
        console.log('handleAvatarSuccess', res)
        this.form.authorAvatar = res.data.url
      },
    }
  }
</script>

<style>
.box {
  margin: 10px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
